import Taro from "@tarojs/taro";

class utils {
  static getSystemInfo() {
    const res = Taro.getSystemInfoSync();
    console.log(res);

    return res;
  }
  static baseUrl = "http://159.75.169.224:7300/pz";
  static http(options) {
    console.log(options);
    if (!options.url) return false;
    if (options.showLoading) {
      this.showLoading();
    }
    return new Promise((resolve, reject) => {
      Taro.request({
        url: this.baseUrl + options.url,
        data: options.data || {},
        header: options.header || {},
        method: options.method || "GET",
        success(res) {
          Taro.hideLoading();
          if (res.data.code !== 10000) {
            if (options.fail && typeof options.fail == "function") {
              options.fail(res.data);
            }
            reject(res.data);
            setTimeout(() => {
              Taro.showToast({
                title: res.data.msg,
                icon: "error",
              });
            }, 2000);
          } else {
            console.log("http", res.data);
            if (options.success && typeof options.success == "function") {
              options.success(res.data);
            }
            resolve(res.data);
          }
        },
        fail(res) {
          Taro.hideLoading();
          reject(res.data);
          if (options.fail && typeof options.fail == "function") {
            options.fail(res.data);
          }
        },
      });
    });
  }
  static showLoading() {
    const isHasLoading = Taro.getStorageSync("loading");
    if (isHasLoading) {
      Taro.hideLoading();
      Taro.setStorageSync("loading", false);
    }
    Taro.showLoading({
      title: "加载中...",
      complete() {
        Taro.setStorageSync("loading", true);
      },
      fail() {
        Taro.setStorageSync("loading", false);
      },
    });
  }
}

export default utils;
